package com.aysafety.iotdataplatformserver.listener;

import com.alibaba.fastjson.JSON;
import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.AcousticEmissionIntensityModel;
import com.aysafety.iotdataplatformserver.dao.AcousticEmissionIntensityDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

// 声发射强度
@Component public class AcousticEmissionIntensityListener {

    private final String type = "AcousticEmissionIntensity";

    private final String tableName = "AcousticEmissionIntensityRealTime";

    private final String[] structure = new String[] {"AcousticEmissionIntensity"};

    private final String calTableName = "CalSoundEmission";

    private final String[] calStructure = new String[] {"warningIndicator", "deviation", "deviationContinued"};

    private final String calWarningTableName = "CalWarningSoundEmission";

    private final String[] calWarningStructure = new String[] {"warning"};
    @Autowired KsTreeDataListener ksTreeDataListener;
    @Autowired AcousticEmissionIntensityDao acousticEmissionIntensityDao;
    private Logger logger = LoggerFactory.getLogger(ErrorListener.class);

    public boolean createTable() throws IOException {
        return HBaseDriver.createTable(this.tableName, this.structure);
    }

    public boolean createCalTable() throws IOException {
        return HBaseDriver.createTable(this.calTableName, this.calStructure);
    }

    public boolean createCalWarningTable() throws IOException {
        return HBaseDriver.createTable(this.calWarningTableName, this.calWarningStructure);
    }

    public void start(Map<String, Object> result) {
        try {
            String clientId = result.get("clientId").toString();
            if (!result.get("dataType").toString().equals("histroy")) {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    AcousticEmissionIntensityModel acousticEmissionIntensity = this.transformData(listData);
                    this.save(acousticEmissionIntensity, clientId);
                }
            } else {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    AcousticEmissionIntensityModel acousticEmissionIntensity = this.transformData(listData);
                    this.save(acousticEmissionIntensity, clientId);
                }
            }
        } catch (IOException e) {
            logger.info("AcousticEmissionIntensityListener:", e);
        }
    }

    public boolean save(AcousticEmissionIntensityModel acousticEmissionIntensity, String clientId) throws IOException {
        String timestamp = String.valueOf(acousticEmissionIntensity.getDatetime().getTime());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableId", acousticEmissionIntensity.getEntId() + "." + "acousticEmissionIntensityRealTime");
        map.put("pk", "\'" + timestamp + ".:." + acousticEmissionIntensity.getDeviceId() + "\'");
        map.put("clientId", "\'" + clientId + "\'");
        map.put("deviceId", "\'" + acousticEmissionIntensity.getDeviceId() + "\'");
        map.put("entId", "\'" + acousticEmissionIntensity.getEntId() + "\'");
        map.put("unit", "\'" + acousticEmissionIntensity.getUnit() + "\'");
        map.put("workingFace", "\'" + acousticEmissionIntensity.getWorkingFace() + "\'");
        map.put("datetime", acousticEmissionIntensity.getDatetime().getTime());
        map.put("monitoringValue", acousticEmissionIntensity.getMonitoringValue());
        map.put("poklad", acousticEmissionIntensity.getPoklad());
        map.put("smallestRange", acousticEmissionIntensity.getSmallestRange());
        map.put("maximumRange", acousticEmissionIntensity.getMaximumRange());
        map.put("stationName", "\'" + acousticEmissionIntensity.getStationName() + "\'");
        map.put("status", acousticEmissionIntensity.isStatus());
        map.put("powerCutArea", "\'" + acousticEmissionIntensity.getPowerCutArea() + "\'");
        map.put("powerReconnectionLimit", acousticEmissionIntensity.getPowerReconnectionLimit());
        map.put("powerCutLimit", acousticEmissionIntensity.getPowerCutLimit());
        acousticEmissionIntensityDao.insertAcousticEmissionIntensityRealTime(map);
        ksTreeDataListener.generateTreeData(acousticEmissionIntensity.getEntId(), this.type,
            acousticEmissionIntensity.getWorkingFace(), acousticEmissionIntensity.getDeviceId(),
            acousticEmissionIntensity.getDatetime(), "");
        return true;
    }

    public AcousticEmissionIntensityModel transformData(Map<String, Object> result) {
        AcousticEmissionIntensityModel acousticEmissionIntensity = new AcousticEmissionIntensityModel();
        if (null != result.get("entId")) {
            acousticEmissionIntensity.setEntId(result.get("entId").toString());
        } else {
            acousticEmissionIntensity.setEntId("无数据");
        }
        if (null != result.get("deviceId")) {
            acousticEmissionIntensity.setDeviceId(result.get("deviceId").toString());
        } else {
            acousticEmissionIntensity.setDeviceId("无数据");
        }
        acousticEmissionIntensity.setMonitoringValue(Double.valueOf(result.get("monitoringValue").toString()));
        if (null != result.get("poklad")) {
            acousticEmissionIntensity.setPoklad(Double.valueOf(result.get("poklad").toString()));
        } else {
            acousticEmissionIntensity.setPoklad(-1.0);
        }
        acousticEmissionIntensity.setDatetime(DateUtils.fomatDateTime(Long.valueOf(result.get("datetime").toString())));
        if (null != result.get("workingFace")) {
            acousticEmissionIntensity.setWorkingFace(result.get("workingFace").toString());
        } else {
            acousticEmissionIntensity.setWorkingFace("无数据");
        }
        if (null != result.get("unit")) {
            acousticEmissionIntensity.setUnit(result.get("unit").toString());
        } else {
            acousticEmissionIntensity.setUnit("无数据");
        }
        if (null != result.get("smallestRange")) {
            acousticEmissionIntensity.setSmallestRange(Double.valueOf(result.get("smallestRange").toString()));
        } else {
            acousticEmissionIntensity.setSmallestRange(-1.0);
        }
        if (null != result.get("maximumRange")) {
            acousticEmissionIntensity.setMaximumRange(Double.valueOf(result.get("maximumRange").toString()));
        } else {
            acousticEmissionIntensity.setSmallestRange(-1.0);
        }
        if (null != result.get("stationName")) {
            acousticEmissionIntensity.setStationName(result.get("stationName").toString());
        } else {
            acousticEmissionIntensity.setUnit("无数据");
        }
        if (null != result.get("status")) {
            if (result.get("status").toString().equals("1")) {
                acousticEmissionIntensity.setStatus(TRUE);
            } else {
                acousticEmissionIntensity.setStatus(FALSE);
            }
        } else {
            acousticEmissionIntensity.setStatus(FALSE);
        }
        if (null != result.get("powerCutArea")) {
            acousticEmissionIntensity.setPowerCutArea(result.get("powerCutArea").toString());
        } else {
            acousticEmissionIntensity.setPowerCutArea("无数据");
        }
        if (null != result.get("powerReconnectionLimit")) {
            acousticEmissionIntensity
                .setPowerReconnectionLimit(Double.valueOf(result.get("powerReconnectionLimit").toString()));
        } else {
            acousticEmissionIntensity.setPowerReconnectionLimit(-1.0);
        }
        if (null != result.get("powerCutLimit")) {
            acousticEmissionIntensity.setPowerCutLimit(Double.valueOf(result.get("powerCutLimit").toString()));
        } else {
            acousticEmissionIntensity.setPowerCutLimit(-1.0);
        }
        return acousticEmissionIntensity;
    }
}
